package com.hqyj.zss.service.impl;

import com.hqyj.zss.dao.CustomerDao;
import com.hqyj.zss.pojo.Customer;
import com.hqyj.zss.service.CustomerService;
import com.hqyj.zss.utils.MD5Util;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerDao customerDao;
    @Autowired
    private MD5Util md5Util;

    public Customer getOneById(String id) {
        Customer oneCustomerById = customerDao.getOneCustomerById(id);
        return oneCustomerById;
    }
    /**
     * 用户查询自己的信息
     * @param username
     * @return
     */
    @Override
    public Customer getOneByUserName(String username) {
        Customer oneCustomer = customerDao.getOneCustomerByUsername(username);
        return oneCustomer;
    }

    /**
     * 修改用户信息
     * @param customer
     * @return 表示修改是否成功
     */
    @Override
    public int modifyCustomerInfo(Customer customer) {
        /* MD5散列算法加密  密码*/
        customer.setC_password(md5Util.md5Hash(customer.getC_password(),customer.getC_username()));
        int i = customerDao.updateCustomer(customer);
        return i;
    }
    /**
     * 使用shiro框架实现登录
     * @param username
     * @param password
     * @return
     */
    @Override
    public Map<String, Object> customerLogin(String username, String password) {
        Map<String,Object> result = new HashMap<>();
        //获取当前用户
        Subject subject = SecurityUtils.getSubject();
        //判断当前用户是否已经认证过
        if (!subject.isAuthenticated()){
            //创建一个认证令牌
            UsernamePasswordToken token = new UsernamePasswordToken(username,password);
            //做登陆
            try{
                subject.login(token);
            }catch(UnknownAccountException e){
                //未知账号异常
                result.put("code",-1);
                result.put("message",username+"用户不存在");
                return result;
            }catch (IncorrectCredentialsException e){
                //密码错误
                result.put("code",-2);
                result.put("message",username+"密码错误");
                return result;
            }catch (AuthenticationException e){
                result.put("code",-10);
                result.put("message","认证失败");
                return result;
            }
        }
        //认证已通过，获取shiro session域中的用户信息
        Session session = subject.getSession();
        Object loginUser = session.getAttribute("loginUser");

        result.put("code",0);
        result.put("message",username+"认证成功");
        //直接返回给控制器方法
        result.put("loginUser",loginUser);

        return result;
    }

    /**
     * 实现登出
     * @return
     */
    @Override
    public Map<String, Object> customerLogout() {
        //使用shiro框架获取当前用户
        Subject subject = SecurityUtils.getSubject();
        //使用shiro框架对当前用户登出
        subject.logout();
        Map<String,Object> result = new HashMap<>();
        result.put("code",0);
        result.put("message","成功登出");
        return result;
    }

    /**
     * 顾客注册
     * @param customer
     * @return
     */
    @Override
    public int insertOne(Customer customer) {
        /* MD5散列算法加密  密码*/
        customer.setC_password(md5Util.md5Hash(customer.getC_password(),customer.getC_username()));
        int i = customerDao.insertOneCustomer(customer);
        return i;
    }

    /**
     * 查询全部用户账号
     * @return
     */
    @Override
    public List<String> queryAllUsername() {
        List<String> usernameList = customerDao.queryAllUsername();
        return usernameList;
    }
}
